import {UserActions} from './user';

const initialState = {};

export const SystemMessageActions = {
  SYSTEM_MESSAGE_UPDATE: 'system_message_update',
  SYSTEM_MESSAGE_BATCH_UPDATE: 'system_message_batch_update',
  SYSTEM_MESSAGE_RESET: 'system_message_reset',
};

export function updateSystemMessage(payload) {
  return {
    type: SystemMessageActions.SYSTEM_MESSAGE_UPDATE,
    payload,
  };
}

export function batchUpdateSystemMessage(payload) {
  return {
    type: SystemMessageActions.SYSTEM_MESSAGE_BATCH_UPDATE,
    payload,
  };
}

export function resetSystemMessage() {
  return {type: SystemMessageActions.SYSTEM_MESSAGE_RESET};
}

export default function systemMessageList(state = initialState, action) {
  switch (action.type) {
    case SystemMessageActions.SYSTEM_MESSAGE_UPDATE:
      const msg = {
        [action.payload.id]: {...state[action.payload.id], ...action.payload},
      };
      return {...state, [action.payload.id]: msg};
    case SystemMessageActions.SYSTEM_MESSAGE_BATCH_UPDATE:
      action.payload.forEach(msg => {
        state[msg.id] = msg;
      });
      return {...state};
    case SystemMessageActions.SYSTEM_MESSAGE_RESET:
      return {};
    case UserActions.USER_SIGN_OUT:
      return {};
    default:
      return state;
  }
}
